<template>
  <el-row :gutter="10">
    <el-col :span="5">
      <el-card>
        <OrgTree authority @node-click="handleOrgClick" />
      </el-card>
    </el-col>
    <el-col :span="19">
      <el-card class="layout-query">
        <el-form ref="queryRef" :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
          <!-- <el-form-item prop="sourceName">
            <el-input v-model="state.queryForm.sourceName" placeholder="货源名称"></el-input>
          </el-form-item>
          <el-form-item prop="sourceType">
            <el-input v-model="state.queryForm.sourceType" placeholder="货源类型"></el-input>
          </el-form-item>
          <el-form-item prop="teamId">
            <el-input v-model="state.queryForm.teamId" placeholder="车队ID"></el-input>
          </el-form-item> -->
          <!-- <el-form-item prop="driverId">
            <el-input v-model="state.queryForm.driverId" placeholder="司机ID"></el-input>
          </el-form-item>
          <el-form-item prop="carId">
            <el-input v-model="state.queryForm.carId" placeholder="车ID"></el-input>
          </el-form-item> -->
          <el-form-item label="矿区" prop="mineId">
            <el-select v-model="state.queryForm.mineId" placeholder="请选择" clearable>
              <el-option v-for="mine in mineList" :key="mine.id" :label="mine.name" :value="mine.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <ma-dict-select
              v-model="state.queryForm.status"
              dict-type="waybill_state"
              clearable
              placeholder="请选择"></ma-dict-select>
          </el-form-item>
          <el-form-item label="模糊KEY" prop="key">
            <el-input v-model="state.queryForm.key" placeholder="模糊关键字 车辆或司机等"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button icon="Search" type="primary" @click="getDataList()">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button icon="RefreshRight" @click="reset(queryRef)">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <el-card>
        <el-space>
          <el-space>
            <el-button v-auth="'biz:waybill:save'" icon="Plus" type="primary" @click="addOrUpdateHandle()"
              >新增</el-button
            >
          </el-space>
          <el-space>
            <el-button v-auth="'biz:waybill:delete'" icon="Delete" plain type="danger" @click="deleteBatchHandle()"
              >批量删除</el-button
            >
          </el-space>
        </el-space>
        <el-table
          v-loading="state.dataListLoading"
          :data="state.dataList"
          border
          class="layout-table"
          @selection-change="selectionChangeHandle">
          <el-table-column type="selection" header-align="center" align="center" width="50" />
          <el-table-column prop="sysOrgName" label="公司/车队" header-align="center" align="center" width="160" />
          <el-table-column prop="mineName" label="装货矿区" header-align="center" width="130" align="center" />
          <el-table-column prop="plateNo" label="车辆" header-align="center" align="center" width="100" />
          <el-table-column prop="driverName" label="司机" header-align="center" align="center" width="100" />
          <el-table-column prop="planningTime" label="计划拉运时间" header-align="center" align="center" />
          <!-- <el-table-column
            prop="sourceName"
            label="货源名称"
            header-align="center"
            align="center"></el-table-column>
          <el-table-column
            prop="sourceNo"
            label="货源编号"
            header-align="center"
            align="center"></el-table-column>
          <el-table-column
            prop="sourceType"
            label="货源类型"
            header-align="center"
            align="center"></el-table-column>
          <el-table-column
            prop="goodsType"
            label="货物类型"
            header-align="center"
            align="center"></el-table-column> -->
          <el-table-column prop="startTime" label="实际开始时间" header-align="center" align="center"></el-table-column>
          <el-table-column prop="endTime" label="实际卸货时间" header-align="center" align="center"></el-table-column>
          <ma-dict-column prop="status" label="运单状态" dict-type="waybill_state"></ma-dict-column>
          <ma-dict-column prop="haveAlarm" label="报警" dict-type="waybill_alarm"></ma-dict-column>
          <ma-dict-column prop="connSt" label="设备状态" dict-type="gps_device_st"></ma-dict-column>
          <el-table-column label="操作" fixed="right" header-align="center" align="center" width="200">
            <template #default="scope">
              <el-button v-auth="'biz:waybill:update'" type="primary" link @click="queryHandle(scope.row)"
                >查看</el-button
              >
              <el-button
                v-if="scope.row.status <= '5'"
                v-auth="'biz:waybill:update'"
                type="danger"
                link
                @click="disuse(scope.row.id)"
                >废弃</el-button
              >
              <!--
            <el-button
              v-auth="'biz:waybill:delete'"
              type="primary"
              link
              @click="deleteBatchHandle(scope.row.id)"
              >删除</el-button
            > -->
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="state.page"
          :page-sizes="state.pageSizes"
          :page-size="state.limit"
          :total="state.total"
          layout="total, sizes, prev, pager, next"
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle">
        </el-pagination>

        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update ref="addOrUpdateRef" @refresh-data-list="getDataList"></add-or-update>

        <Detail ref="detailRef"></Detail>
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
import { useCrud } from '@/hooks'
import { reactive, ref } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import AddOrUpdate from './add-or-update.vue'
import OrgTree from '@/views/sys/user/org-tree.vue'
import Detail from '@/views/biz/waybill/detail.vue'
import { useRouter } from 'vue-router'
import MaDictColumn from '@/components/ma-dict/ma-dict-column/index.vue'
import { useMineList } from '@/api/biz/mine'
import { disuseApi } from '@/api/biz/waybill'
import { ElMessageBox } from 'element-plus'

const state: IHooksOptions = reactive({
  dataListUrl: '/biz/waybill/page',
  deleteUrl: '/biz/waybill',
  responseInterceptor: (data) => {
    data?.forEach((value, index) => {
      data[index].haveAlarm = value.haveAlarm.toString()
    })
    return data
  },
  queryForm: {
    startDateTime: '',
    endDateTime: '',
    sourceName: '',
    sourceType: '',
    teamId: '',
    driverId: '',
    carId: '',
    mineId: '',
    status: '',
    sysOrgId: '',
    key: '',
    order: 'create_time',
    asc: false
  }
})
const mineList = ref<any[]>([])
const router = useRouter()

const queryRef = ref()
const addOrUpdateRef = ref()
const detailRef = ref()
const addOrUpdateHandle = (id?: number) => {
  addOrUpdateRef.value.init(id)
}

function queryHandle({ sysOrgId, id }) {
  router.push({
    path: '/dashboard/waybillMonitor',
    query: {
      sysOrgId,
      id
    }
  })
}

//废弃

function disuse(id) {
  console.log(id)
  ElMessageBox.confirm('确定进行操作?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    disuseApi(id).then(() => {
      // ElMessage.success('废弃成功')
      getDataList()
    })
  })
}

const handleOrgClick = (orgId: any) => {
  state.queryForm.sysOrgId = orgId
  getDataList()
}
function getMineList() {
  useMineList().then((res) => {
    mineList.value = res.data
  })
}
getMineList()
const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle, reset } =
  useCrud(state)
</script>
